$().ready(function(){
    let page = {
        index_:0
    };
    function init(){
        getInfo(page.index_);
    }
    init();

    //获取详情
    function getInfo(index_){
        let url = '';
        let info = {};
        getAjax(`${global.apiUrl}/Api/Admincommunity/get_yl_content`,res=>{
            console.log(res);
            if(index_ == 0){
                info = res.one;
            }
            if(index_ == 1){
                info = res.two;
            }
            if(index_ == 2){
                info = res.three;
            }
            //初始化页面
            $('#title').val(info.title);
            let html = '';
            if(info.sub_content){
                for(let i in info.sub_content){
                    html += `
                 <div class="sub-content">
                    <input type="text" placeholder="请输入简要内容" value="${info.sub_content[i].item}" class="sub-content-item">
                    <span class="delete-sub">删除</span>
                </div>
                `
                }
            }else{
                html = `
                    <div class="sub-content">
                    <input type="text" placeholder="请输入简要内容" class="sub-content-item">
                    <span class="delete-sub">删除</span>
                </div>
                `
            }

            $('#sub-content-box').html(html);
            $('.summernote').code(info.content)

        },{
            community_id:global.community_id
        })
    }

    //新增简要
    $('#add_sub_content_btn').on('click',function(){
        let html = `
            <div class="sub-content">
                <input type="text" placeholder="请输入简要内容" class="sub-content-item" />
                <span class="delete-sub">删除</span>
            </div>
        `
        $('#sub-content-box').append(html);
    })
    //删除当前简要
    $('#sub-content-box').on('click','.delete-sub',function(){
        let parentBox = $(this).parent();
        // console.log(parentBox);
        parentBox.remove();
    })
    //导航切换
    $('.nav-item').on('click', function () {
        $('.nav-item').removeClass('active');
        $(this).addClass('active');
        var index = $(this).index();
        console.log(index);
        $('.item-box').css('display', 'none');
        // var itemBox = document.getElementsByClassName('item-box');
        // itemBox[index].style.display = 'block';
        $('.item-box').eq(index).css('display','block');
        page.index_ = index;
        getInfo(page.index_);
    })
    //富文本
    $('.summernote').summernote({
        height: 300,
        lang: 'zh-CN',
        onImageUpload: function(files, editor, $editable) {
            sendFile(files[0],editor,$editable);
        }
    })
    $('.summernote').code('');
    //提交修改
    function operationInfo(index_){
        let url = '';
        if(index_ == 0){
            url = '/Api/Admincommunity/operation_yl_one'
        }
        if(index_ == 1){
            url = '/Api/Admincommunity/operation_yl_two'
        }
        if(index_ == 2){
            url = '/Api/Admincommunity/operation_yl_three'
        }
        let pro = {};
        let title = $('#title').val();
        pro.title = title;
        let sub_content = [];
        $('.sub-content-item').each(function(key,value){
            let item = {};
            item.item = $(this).val();
            sub_content.push(item);
        })
        pro.sub_content = sub_content;
        let content = $('.summernote').code();
        pro.content = content;
        pro.community_id = global.community_id;
        pro = JSON.stringify(pro);
        console.log(pro);
        getAjax(`${global.apiUrl+url}`,res=>{
            console.log(res);
            if(res == 1){
                alert('信息提交成功！');
            }
        },{
            type:1,
            pro:pro
        })
    }
    $('#sub-btn').on('click',function(){
        operationInfo(page.index_);
    })
})
